package main

import (
	"fmt"
	"github.com/jinzhu/gorm"
	_ "github.com/jinzhu/gorm/dialects/mysql"
)

type User struct {
	gorm.Model
	CreditCards []CreditCard
}
type CreditCard struct {
	gorm.Model
	Number string
	UserID uint
}

func main() {
	db, err := gorm.Open("mysql", "root:123456@(127.0.0.1:3306)/gin?charset=utf8mb4&parseTime=True&loc=Local")
	if err != nil {
		fmt.Println(err)
	}
	defer db.Close()

	//自动迁移
	//db.AutoMigrate(&User{}, &CreditCard{})

	//添加记录

	//db.Create(&User{
	//	CreditCards: []CreditCard{
	//		{
	//			Number: "C++",
	//		},
	//		{
	//			Number: "高性能golang",
	//		},
	//	},
	//})
	//db.Create(&CreditCard{
	//	Number: "mysql原理",
	//	UserID: 1,
	//})
	//u := User{}
	//db.Preload("CreditCards").First(&u)
	//fmt.Println(u)

	//c := CreditCard{}
	//db.First(&c)
	//fmt.Println(c)

}
